import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../views/login'
import layout from '../views/layout'
import notfound from '../views/notfound'
import home from '../views/home'
import productlist from '../views/pms/productlist'
import productadd from '../views/pms/productadd'
import producttype from '../views/pms/producttype'
import productcate from '../views/pms/productcate'
import productbrand from '../views/pms/productbrand'
import Cookies from 'js-cookie'
//使用插件   Vue使用Vue路由插件
Vue.use(VueRouter)

const routes = [
	{
		path:"/",
		redirect:"/home",
		meta:{
			isMenu:false,
			name:"首页"
		}
	},
	{
		path:"/login",
		component:login,
		meta:{
			isMenu:false,
			name:"登录"
		}
	},
	{
		path:"/layout",
		component:layout,
		meta:{
			isMenu:false
		},
		children:[
			{path:"/home",component:home}
		]
	},
	//独立的商品模块，所以我把他独立出来配置
	{
		path:"/pms",
		component:layout,
		meta:{
			isMenu:true,
			name:"商品",
			icon:"iconfont icon-shangpin-tianchong"
		},
		children:[
			{
				path:"list",
				component:productlist,
				meta:{
					isMenu:true,
					name:"商品列表",
					icon:"iconfont icon-liebiao"
				}
			},	//  /pms/productlist
			{
				path:"add",
				component:productadd,
				meta:{
					isMenu:true,
					name:"添加商品",
					icon:"el-icon-s-goods"
				}
			},
			{
				path:"cate",
				component:productcate,
				meta:{
					isMenu:true,
					name:"商品分类",
					icon:"el-icon-remove"
				}
			},
			{
				path:"type",
				component:producttype,
				meta:{
					isMenu:true,
					name:"商品类型",
					icon:"el-icon-location"
				}
			},
			{
				path:"brand",
				component:productbrand,
				meta:{
					isMenu:true,
					name:"品牌管理",
					icon:"el-icon-location"
				}
			}
		]
	},
	{
		path:"*",
		component:notfound,
		meta:{
			isMenu:false
		}
	}
]

const router = new VueRouter({
  // mode: 'history',
  // base: process.env.BASE_URL,
  routes
})


//添加路由守卫。
router.beforeEach((to,from,next)=>{
	//如果要去登录，直接放行
	if(to.path == "/login"){
		next();
	}else{
	//否则就要看是否有令牌(已经登录),有令牌，放行，否则，去登录
		//查看令牌--令牌在cookie中
		let token = Cookies.get("token");
		if(token){
			//登录过，放行
			next();
		}else{
			next("/login");
		}
	}
})

export default router
